package com.tdzx.ui;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.map.TransitOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.tdzx.R;

public class RoutePlanActivity extends BaseActivity_NoBar {

//	Button mBtnDrive = null;	// 驾车搜索
//	Button mBtnTransit = null;	// 公交搜索
//	Button mBtnWalk = null;	// 步行搜索
//	Button mBtnCusRoute = null; //自定义路�?
	
	MapView mMapView = null;	// 地图View
	MKSearch mSearch = null;	// 搜索模块，也可去掉地图模块独立使�?
	
	ProgressDialog progressDialog;
	
	MyLocationOverlay myLocation;
	
	UILApplication app;
	
	
	ImageView caterory_back;
	ImageView cat_src;
	
	String title;
	int status = 0;
	
	String endAddress;

	TextView luxian;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        app = (UILApplication)this.getApplication();
		if (app.mBMapManager == null) {
			app.mBMapManager = new BMapManager(this);
			app.mBMapManager.init(UILApplication.strKey,new UILApplication.MyGeneralListener());
		}
		setContentView(R.layout.websiteroute);
        mMapView = (MapView)findViewById(R.id.bmapsView2);
        
        initView();
        
        initMapView();
        
        // 初始化搜索模块，注册事件监听
        mSearch = new MKSearch();
        mSearch.init(app.mBMapManager, new MKSearchListener(){

            @Override
            public void onGetPoiDetailSearchResult(int type, int error) {
            }
            
			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
				if(!isFinishing()) {
					
			
				// 错误号可参�?MKEvent中的定义
				if (error != 0 || res == null) {
					Toast.makeText(RoutePlanActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
					finishProgress();
					mMapView.getController().animateTo(new GeoPoint((int)(app.locData.latitude * 1E6), ((int)(app.locData.longitude * 1E6))));
					return;
				}
				RouteOverlay routeOverlay = new RouteOverlay(RoutePlanActivity.this, mMapView);
			    // 此处仅展示一个方案作为示�?
			    routeOverlay.setData(res.getPlan(0).getRoute(0));
			    mMapView.getOverlays().clear();
			    mMapView.getOverlays().add(routeOverlay);
			    
			    /*myLocation = new MyLocationOverlay(mMapView);
		        myLocation.setData(app.locData);
		        mMapView.getOverlays().add(myLocation);
		        myLocation.enableCompass();*/
			    
			    mMapView.refresh();
			    // 使用zoomToSpan()绽放地图，使路线能完全显示在地图�?
			    mMapView.getController().zoomToSpan(routeOverlay.getLatSpanE6(), routeOverlay.getLonSpanE6());
			    mMapView.getController().animateTo(res.getStart().pt);
			    finishProgress();
			    
				}
			}

			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(RoutePlanActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
					finishProgress();
					mMapView.getController().animateTo(new GeoPoint((int)(app.locData.latitude * 1E6), ((int)(app.locData.longitude * 1E6))));
					return;
				}
				TransitOverlay  routeOverlay = new TransitOverlay (RoutePlanActivity.this, mMapView);
			    // 此处仅展示一个方案作为示�?
			    routeOverlay.setData(res.getPlan(0));
			    mMapView.getOverlays().clear();
			    mMapView.getOverlays().add(routeOverlay);
			    mMapView.refresh();
			    // 使用zoomToSpan()绽放地图，使路线能完全显示在地图�?
			    mMapView.getController().zoomToSpan(routeOverlay.getLatSpanE6(), routeOverlay.getLonSpanE6());
			    mMapView.getController().animateTo(res.getStart().pt);
			    finishProgress();
			}

			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
				if (error != 0 || res == null) {
					Toast.makeText(RoutePlanActivity.this, "抱歉，未找到结果", Toast.LENGTH_SHORT).show();
					finishProgress();
					mMapView.getController().animateTo(new GeoPoint((int)(app.locData.latitude * 1E6), ((int)(app.locData.longitude * 1E6))));
					return;
				}
				RouteOverlay routeOverlay = new RouteOverlay(RoutePlanActivity.this, mMapView);
			    // 此处仅展示一个方案作为示�?
				routeOverlay.setData(res.getPlan(0).getRoute(0));
			    mMapView.getOverlays().clear();
			    mMapView.getOverlays().add(routeOverlay);
			    mMapView.refresh();
			    // 使用zoomToSpan()绽放地图，使路线能完全显示在地图�?
			    mMapView.getController().zoomToSpan(routeOverlay.getLatSpanE6(), routeOverlay.getLonSpanE6());
			    mMapView.getController().animateTo(res.getStart().pt);
			    finishProgress();
			    
			}
			public void onGetAddrResult(MKAddrInfo res, int error) {
			}
			public void onGetPoiResult(MKPoiResult res, int arg1, int arg2) {
			}
			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
			}

			@Override
			public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
			}

        });
        
        Intent intent = getIntent();
        int latitude = intent.getIntExtra("latitude", 0);
        int longtitude = intent.getIntExtra("longtitude", 0);
        
        title = intent.getStringExtra("luxian");
        luxian.setText(title);
        
        status = intent.getIntExtra("status", status);
        
        endAddress = intent.getStringExtra("endAddress");
        
        GeoPoint start = new GeoPoint((int)(app.locData.latitude * 1E6), (int)(app.locData.longitude * 1E6));
        GeoPoint end = new GeoPoint(latitude, longtitude);
        SearchButtonProcess(null, start, end,status);
        showProgress("正在加载路线...");
	}
	
	private void initView() {
		luxian = (TextView) findViewById(R.id.luxian);
		
		caterory_back = (ImageView) findViewById(R.id.caterory_back);
		caterory_back.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				Log.i("ccc", "onTouch ca");
				cat_src.onTouchEvent(arg1);
				return false;
			}
		});
		
		caterory_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		cat_src = (ImageView) findViewById(R.id.cat_src);
		cat_src.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.i("ccc", "onTouch crc");
				return true;
			}
		});
	}
	
	private void initMapView() {
	    mMapView.getController().enableClick(true);
	    mMapView.getController().setZoom(14);
	    mMapView.setBuiltInZoomControls(false);
	    mMapView.setDoubleClickZooming(true);  
	    
	}
	
	
	@Override
    protected void onDestroy() {
        mMapView.destroy();
        finishProgress();
        super.onDestroy();
    }
	
	void SearchButtonProcess(View v,GeoPoint start,GeoPoint end,int status) {
		// 对起点终点的name进行赋�?，也可以直接对坐标赋值，赋�?坐标则将根据坐标进行搜索
		MKPlanNode stNode = new MKPlanNode();
//		stNode.name = editSt.getText().toString();
		stNode.pt = start;
		MKPlanNode enNode = new MKPlanNode();
//		enNode.name = editEn.getText().toString();
		enNode.pt = end;
		if(status == 0) {
			mSearch.drivingSearch("", stNode, "", enNode);
		} else if(status == 1) {
			MKPlanNode stNode_1 = new MKPlanNode();
			stNode_1.name = UILApplication.currenrCityName;
			MKPlanNode enNode_1 = new MKPlanNode();
			enNode_1.name = endAddress;
			
			mSearch.transitSearch("宜兴", stNode, enNode);
		} else if(status == 2) {
			mSearch.walkingSearch("宜兴", stNode, "宜兴", enNode);
		}

	}

    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	mMapView.onSaveInstanceState(outState);
    	
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    	super.onRestoreInstanceState(savedInstanceState);
    	mMapView.onRestoreInstanceState(savedInstanceState);
    }
    
    protected void showProgress(String message) {
		progressDialog = new ProgressDialog(RoutePlanActivity.this);
		progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		progressDialog.setMessage(message);
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				finish();
			}
		});
		progressDialog.show();
	}
	
	/**
	 * 结束进度�?
	 * 
	 * @param progress
	 */
	protected void finishProgress() {
		if (progressDialog != null) {
			progressDialog.dismiss();
			progressDialog=null;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			finish();
//			overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
		}
		return super.onKeyDown(keyCode, event);
	}

}
